<template>
	<view>
		<u-popup :show="show" :round="10" @close="close">

			<view class="popupBody" :style="{height:'25vh'}">
				<view class="userAddBox">
					<view class="userAvatar">
						<view class="upload" v-if="uploadDisabled">
							<u-loading-icon vertical textSize='30rpx' text='上传中...' size='100rpx' textColor='#fff'
								color='#fff'></u-loading-icon>
						</view>
						<u-avatar mode='aspectFill' :src="avatar" size='200rpx' @click="upload"></u-avatar>
					</view>
					<view class="viewWidth">
						<u-input v-model="nick_name" placeholder='请输入用户名' />
					</view>
				</view>
			</view>
			<view class="viewWidth "  style="display: flex;align-items: center;">
				<u-button :disabled='disabled||uploadDisabled' @click='type="search"'>
					取消
				</u-button>
				<u-button :disabled='disabled||uploadDisabled' @click='addUser' color="#3975c6">
					确认
				</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		props: ['show'],
		data() {
			return {
				uploadDisabled: false,
				nick_name: null,
				avatar: null,
				disabled: false,
			}
		},
		methods: {
			upload() {
				if (this.uploadDisabled) return
				uni.chooseImage({
					count: 1,
					success: (res) => {
						this.avatar = res.tempFilePaths[0]
						this.uploadDisabled = true
						uni.uploadFile({
							url: this.$config.url + '/api/image/upload', //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							header: {
								'Content-Type': 'application/json;charset=UTF-8',
								'Authorization': "Bearer " + uni.getStorageSync('token')
							},
							success: (res) => {
								this.avatar = JSON.parse(res.data).data.data.urlMedium
								this.uploadDisabled = false
							},
							fail: (err) => {
								this.uploadDisabled = false
							}
						});
					},
					fail: () => {

						this.uploadDisabled = false
					}
				})
			},
			addUser() {
				if (!this.nick_name) {
					uni.$u.toast('请输入家庭名称')
					return
				}
				if (!this.avatar) {
					uni.$u.toast('请上传头像')
					return
				}
				this.disabled = true
				uni.showLoading({
					title: '添加中...'
				})
				this.$api.My.Family.Add({
					title: this.nick_name,
					avatar: this.avatar
				}).then(res => {
					uni.hideLoading()
					this.disabled = false
					this.close()
					this.$emit('reload')
				}).catch(err => {
					uni.$u.toast(err.data.message)
					uni.hideLoading()
					this.disabled = false
				})
			},

			close() {
				if (this.uploadDisabled) return
				if (this.disabled) return
				this.$emit('close')
			},

		}
	}
</script>

<style lang="scss" scoped>
	.userInfoBox {
		display: flex;
		margin-bottom: 20rpx;
		align-items: center;
		width: 100%;

		.userContent {
			margin-left: 10px;
			flex: 1;
			display: flex;
			height: 100rpx;
			flex-direction: column;
			justify-content: space-between;
			border-bottom: 1rpx solid #f1f1f1;
			padding: 0 0 10rpx 0;
		}
	}

	.shareBtn {
		width: 30vw;
		margin: 40rpx auto;
		border-radius: 100rpx;
		font-size: 24rpx;
		border: 1rpx solid #bbb;
	}

	.userAddBox {
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: space-around;
		flex-direction: column;
	}

	.userAvatar {
		position: relative;
		width: 200rpx;
		height: 200rpx;
		border-radius: 100%;
		overflow: hidden;

		.upload {
			position: absolute;
			z-index: 1;
			width: 100%;
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>